package com.ruoyi.web.controller.wfshou;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wfshou.domain.Proj;
import com.ruoyi.wfshou.service.IProjService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.*;

/**
 * 科研项目Controller
 * 
 * @author zongying
 * @date 2023-06-20
 */
@RestController
@RequestMapping("/wfshou/proj")
public class ProjController extends BaseController
{
    @Autowired
    private IProjService projService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询科研项目列表
     */
    @PreAuthorize("@ss.hasPermi('wfshou:proj:list')")
    @GetMapping("/list")
    public TableDataInfo list(Proj proj)
    {
        startPage();
        List<Proj> list = projService.selectProjList(proj);
        return getDataTable(list);
    }
    @GetMapping("/listByUserId")
    public TableDataInfo listByUserId()
    {
        Long userId=SecurityUtils.getUserId();
        List<Proj> list = projService.selectProjListByUserId(userId);
        return getDataTable(list);
    }
    /**
     * 导出科研项目列表
     */
    @PreAuthorize("@ss.hasPermi('wfshou:proj:export')")
    @Log(title = "科研项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Proj proj)
    {
        List<Proj> list = projService.selectProjList(proj);
        ExcelUtil<Proj> util = new ExcelUtil<Proj>(Proj.class);
        util.exportExcel(response, list, "科研项目数据");
    }

    /**
     * 获取科研项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('wfshou:proj:query')")
    @GetMapping(value = "/{projId}")
    public AjaxResult getInfo(@PathVariable("projId") Long projId)
    {
        return success(projService.selectProjByProjId(projId));
    }

    /**
     * 新增科研项目
     */
    @PreAuthorize("@ss.hasPermi('wfshou:proj:add')")
    @Log(title = "科研项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Proj proj)
    {
        Long userId=SecurityUtils.getUserId();
        proj.setUserId(userId);
        return toAjax(projService.insertProj(proj));
    }

    /**
     * 修改科研项目
     */
    @PreAuthorize("@ss.hasPermi('wfshou:proj:edit')")
    @Log(title = "科研项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Proj proj)
    {
        if( proj.getProjStatus().equals("1")){
            Long[] roleIds = {Long.valueOf(109)};
            sysUserService.insertUserAuth(proj.getUserId(),roleIds);
        }
        else
        {
            Long[] roleIds = {Long.valueOf(106)};
            sysUserService.insertUserAuth(proj.getUserId(),roleIds);
        }
        return toAjax(projService.updateProj(proj));
    }

    /**
     * 删除科研项目
     */
    @PreAuthorize("@ss.hasPermi('wfshou:proj:remove')")
    @Log(title = "科研项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projIds}")
    public AjaxResult remove(@PathVariable Long[] projIds)
    {
        return toAjax(projService.deleteProjByProjIds(projIds));
    }
}
